<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 共用模块-临时文件上传的Ajax调用接口
 */
class AjaxUpload extends AdminController
{
    function __construct()
    {
        parent::__construct();
        $this->output->enable_profiler(false);
        $this->temp_path = $this->config->item('tmp_path') ? trim($this->config->item('tmp_path'), '/ ') : 'upload/files';
    }

    function index()
    {
        //$this->upload();
    }

    /**
     * 上传文件到临时文件目录
     * @param string $field
     */
    function upload($field)
    {
        if (!isset($_FILES) || !is_array($_FILES) || count($_FILES) < 1) {
            echo json_encode(Array('error' => '请选择要上传的文件！'));
            return;
        }
        $path = $this->temp_path . '/' . date('Y_m_d', time());
        if (!file_exists($path)){
            if (!mkdir($path, DIR_WRITE_MODE,true)) {
                echo json_encode(Array('error' => $path));
                return;
            }
        }
        //上传文件
        $config['upload_path'] = $path . '/';
        $config['allowed_types'] = 'jpg|png|bmp|swf|docx|doc|xls|xlsx|rar|zip';
        $config['max_size'] = '102400';
        $config['overwrite'] = FALSE;
        $this->load->library('upload', $config);
        $file_return = $this->upload->do_upload($field);
        if ($file_return) {
            $data = $this->upload->data();
            $message['path'] = $config['upload_path'] . $file_return;
//            $message['path'] = $config['upload_path'] . $data['file_name'];
            $message['url'] = site_url($message['path']);
            $message['file_size'] = $data['file_size'];
            $message['is_image'] = $data['is_image'];
            $message['error'] = '';
        } else {
            $message['error'] = $this->upload->display_errors('');
        }
        echo json_encode($message);
    }

    function getExtName($file_name)
    {
        $ext = explode('.', $file_name);
        return $ext[1];
    }

    /**
     * 清理过期的临时文件
     */
    private function removeTempFile()
    {
        if (!$paths = @scandir($this->temp_path)) {
            return;
        }
        foreach ($paths as $path) {
            if (!$path || $path[0] == '.') {
                continue;  //隐含目录以及 . 和 .. 都不处理
            }
            $path = $this->temp_path . '/' . $path;
            if (is_dir($path) && (fileatime($path) < time() - 3600 * 12)) {
                foreach (@scandir($path) as $file) {
                    $file = $path . '/' . $file;
                    if (is_file($file)) {
                        @unlink($file);
                    }
                }
                @rmdir($path);
            }
        }
    }
}
